<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\Teacher as TeacherModel;

class Teacher extends Controller
{
	// 显示数据
	public function index ()
	{
		$name = input('get.name');
		$pageSize = 5;
		
		// 用户名模糊搜索
		if(!empty($name)){
			$teachers = db('teacher')->where('name', 'like', '%' . $name . '%')->paginate($pageSize, false, ['query'=>['name'=>$name]]);
		}else{
			$teachers = db('teacher')->paginate($pageSize, false, ['query'=>['name'=>$name]]);
		}
		
		$this->assign('teachers', $teachers);
		return $this->fetch();
	}
	
	// 插入数据页面
	public function add ()
	{
		return view();
	}
	// 插入数据
	public function insert()
    {
    	$Teacher = new TeacherModel;
		
		if($Teacher->allowField(true)->validate(true)->save(input('post.')))
		{
			$this->success('用户[ ' . $Teacher->name . ':' . $Teacher->id . ' ]新增成功', 'index');
		}
		else
		{
			return $Teacher->getError();
		}
    }
	// 删除数据
	public function delete ()
	{
		if(db('teacher')->delete(input('get.id'))){
			$this->success('删除成功', 'index');
		}else{
			$this->error('删除失败', 'index');
		}
	}
	// 修改数据
	public function edit ()
	{
		$teacher = db('teacher')->find(input('get.id'));
		$this->assign('teacher', $teacher);
		return $this->fetch();
	}
	// 提交修改后的数据
	public function update ()
	{
		$Teacher = new TeacherModel;
		
		if($Teacher->allowField(true)->validate(true)->update(input('post.')))
		{
			return $this->success('修改成功','index');
		}
		else
		{
			return $Teacher->getError();
		}
	}
}
